<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <script src="js/jquery-3.6.0.min.js"></script>
</head>

<body style="padding: 15px;">
    <!-- 添加图书的panel面板 -->
    <!-- vscode安装bootstrap 3 snippets插件，使用bs3-panel快捷创建panel面板 -->
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">添加新图书</h3>
        </div>
        <div class="panel-body form-inline">
            <!--form-inline 一行显示-->
            <!-- bs3-input -->
            <div class="input-group">
                <div class="input-group-addon">书名</div>
                <input type="text" class="form-control" id="iptBookname" placeholder="请输入书名">
            </div>
            <div class="input-group">
                <div class="input-group-addon">作者</div>
                <input type="text" class="form-control" id="iptAuthor" placeholder="请输入作者">
            </div>
            <div class="input-group">
                <div class="input-group-addon">出版社</div>
                <input type="text" class="form-control" id="iptPublisher" placeholder="请输入出版社">
            </div>
            <button id="btnAdd" class="btn btn-primary">添加</button>

        </div>
    </div>

    <!-- 图书的表格 -->
    <!-- bs3-table -->
    <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th>Id</th>
                <th>书名</th>
                <th>作者</th>
                <th>出版社</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody id='tb'>
        </tbody>
    </table>

    <script>
        $(function() {

            var rootUrl = 'http://liulongbin.top:3006/';
            // 获取图书列表数据
            function getBookList() {
                $.get(`${rootUrl}api/getbooks`, function(res) {
                    // console.log(res);
                    if (res.status !== 200) return alert('获取数据失败！');
                    let rows = [];
                    // console.log(res.data)
                    $.each(res.data, (index, item) => {
                        console.log(item)
                        rows.push(`<tr><td>${item.id}</td><td>${item.bookname}</td><td>${item.author}</td><td>${item.publisher}</td><td><a href="javascript:;" class="del" data-id="${item.id}">删除</a></td></tr>`)
                    });
                    $('#tb').empty().append(rows.join(''));
                })
            }

            getBookList();

            $('tbody').on('click', '.del', function() {
                let id = $(this).attr('data-id');
                // console.log(id)
                $.get(`${rootUrl}api/delbook`, {
                    id
                }, res => {
                    if (res.status !== 200) return alert('删除图书失败！');
                    getBookList();
                });
            });

            $('#btnAdd').on('click', function() {
                let bookname = $('#iptBookname').val().trim();
                let author = $('#iptAuthor').val().trim();
                let publisher = $('#iptPublisher').val().trim();
                if (!bookname || !author || !publisher) return alert('请填写完整的图书信息！')
                $.post(`${rootUrl}api/addbook`, {
                    bookname,
                    author,
                    publisher
                }, res => {
                    if (res.status !== 201) return alert('新增图书失败！');
                    getBookList();
                    $('#iptBookname').val('');
                    $('#iptAuthor').val('');
                    $('#iptPublisher').val('');
                })
            })
        })
    </script>

</body>

</html>